﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Model;
using System.ComponentModel;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Business;


namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class CashOrderController : BaseController
    {
        [Description("提现列表")]
        [ViewPage]
        public ActionResult GetList(CashOrderQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.View_CashOrder.Where(t => t.is_del == 0);
            if (!string.IsNullOrWhiteSpace(queryModel.account_mobile))
                queryable = queryable.Where(t => t.account_mobile == queryModel.account_mobile);
            if (!string.IsNullOrWhiteSpace(queryModel.member_name))
                queryable = queryable.Where(t => t.member_name.Contains(queryModel.member_name));
            if (!string.IsNullOrWhiteSpace(queryModel.replace_buyer_NO))
                queryable = queryable.Where(t => t.replace_buyer_NO == queryModel.replace_buyer_NO);
            if (!string.IsNullOrWhiteSpace(queryModel.order_no))
                queryable = queryable.Where(t => t.order_no.Contains(queryModel.order_no));
            if (queryModel.status != -10)
                queryable = queryable.Where(t => t.status == queryModel.status);
            if (queryModel.beg_time.HasValue)
                queryable = queryable.Where(t => t.create_time >= queryModel.beg_time);
            if (queryModel.end_time.HasValue)
            {
                var end_time = queryModel.end_time.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.create_time < end_time);
            }

            //分页
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            decimal? cashAmt = queryable.Sum(t => t.cash_amt);
            ViewData.Add("AllCashAmt", cashAmt);
            var querylist = queryable.OrderByDescending(t => t.order_id).ThenByDescending(t => t.create_time)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<View_CashOrder> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<View_CashOrder>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }

        [Description("审核操作")]
        [HttpPost]
        public ActionResult Audit(long order_id, int status, string msg)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var order = dbcontext.ADZ_CashOrder.FirstOrDefault(t => t.order_id == order_id && t.status == (byte)CashOrderStatus.Create);
            if (order != null)
            {
                order.audit_time = DateTime.Now;
                order.audit_user = new CurrentUser().user.loginName;
                order.status = (byte)status;
                order.reason = msg;
                if (dbcontext.SaveChanges() > 0 && status == (int)CashOrderStatus.AuditFail)
                {
                    //审核驳回，退款给用户
                    var chargeMember = dbcontext.ADZ_ChargeMaster.FirstOrDefault(t => t.uid == order.uid);
                    if (chargeMember != null)
                    {
                        //加钱
                        chargeMember.charge_balance += order.cash_amt;
                        chargeMember.account_balance += order.cash_amt;
                        dbcontext.SaveChanges();

                        //记录资金流水
                        AccountBiz.Instance.AddAmountLog((long)order.uid, (decimal)order.cash_amt, (int)OrderType.CashRefund, order.order_no, 0, "提现退款", "提现退款", 0);
                    }
                }
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("汇款")]
        [HttpPost]
        public ActionResult Give(long order_id)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var order = dbcontext.ADZ_CashOrder.FirstOrDefault(t => t.order_id == order_id && t.status == (byte)CashOrderStatus.AuditSucc);
            if (order != null)
            {
                order.status = (int)CashOrderStatus.GiveMoney;
                dbcontext.SaveChanges();
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
    }
}